<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('login_model');
		$this->load->library(array('session','form_validation'));
		$this->load->helper(array('url','form'));
		$this->load->database('default');
		$this->load->model('Usuarios_model','',TRUE);
	}

	public function index()
	{
		switch ($this->session->userdata('perfil')) {
			case '':
				$data['token'] = $this->token();
				$data['titulo'] = 'Login';
				$this->load->view('login_view', $data);
				break;
			case 'admin':
				redirect(base_url().'admin');
				break;
			case 'operador':
				redirect(base_url().'admin');
				break;
			case 'administradorSede':
				redirect(base_url().'admin');
				break;
			default:
				$data['titulo'] = 'Login';
				$this->load->view('login_view', $data);
				break;
		}
	}

	public function token()
	{
		$token = md5(uniqid(rand(),true));
		$this->session->set_userdata('token',$token);
		return $token;
	}

	public function reintentar()
	{
		if($this->input->post('token') && $this->input->post('token') == $this->session->userdata('token'))
		{
			$this->form_validation->set_rules('username', 'nombre de usuario', 'required|trim|min_length[2]|max_length[150]|xss_clean');
			$this->form_validation->set_rules('password', 'password', 'required|trim|min_length[6]|max_length[150]|xss_clean');

			//lanzamos mensajes de error si es que los hay
			$this->form_validation->set_message('required', 'El %s es requerido');
			$this->form_validation->set_message('min_length', 'El %s debe tener al menos %s carácteres');
			$this->form_validation->set_message('max_length', 'El %s debe tener al menos %s carácteres');

			if($this->form_validation->run() == FALSE)
			{
				$this->index();
			}else{
				$username = $this->input->post('username');
				$password = sha1($this->input->post('password'));
				$check_user = $this->login_model->login_user($username,$password);
				//print_r($check_user);
				if($check_user == TRUE)
				{
					$data = array(
				'is_logued_in' => TRUE,
				'id_usuario' =>  $check_user->id,
				'perfil' => $check_user->description,
				'username' => $check_user->username
					);
					$this->session->set_userdata($data);
					$this->index();
				}
			}
		}else{
			redirect(base_url().'login');
		}
	}

	public function newUser()
	{
		$this->load->view('register');
	}
	
	
	public function newUserAdd()
	{
		$usuario = array(
			//Información general
			'nombre' 			=> $this->input->post('name'),
			'apellido' 			=> $this->input->post('surname'),
			'documento' 		=> $this->input->post('dni'),
			'fec_nac' 			=> $this->input->post('fechaNacimiento'),
			'direccion' 		=> $this->input->post('direccion'),
			//Información de sesión
			'nombre_usuario' 	=> $this->input->post('usuario'),
			'email' 			=> $this->input->post('email'),
			'password' 			=> $this->input->post('password'),
			'enable'		 	=> $this->input->post('activo'),
			'id_role'		 	=> "2"	
		);
		$this->Usuarios_model->save($usuario);
			
		$data['message'] = '<div class="success">El usuario fue dado de alta exitosamente</div>';
		$this->load->view('register');
	}

	public function logout_ci()
	{
		$this->session->sess_destroy();
		$this->index();
	}
}
